<?php
function getHtmlRigaRif($n=20) {
	$sHtml = "<tr height='1'>";
	$w = 100/$n;
	for($k=0; $k<$n; $k++) {
		$sHtml .= "<td width='$w%'></td>";
	}
	$sHtml .= "</tr>\n";
	return $sHtml;
}

/**
 * genera campi form da array aCamForm
 * sModo = 	inp -> campi modificabili
 * 			hid -> campi hidden + contenuto visualizzato
 * 			vis -> solo contenuto visualizzato
 */
function getHtmlCampiForm($aCamForm, $sModo='inp') {
	$sHtml = "";
	foreach($aCamForm as $rX => $aRX) {
	   $sHtml .= "\t<tr>\n";
		foreach($aRX as $cc => $aCX) {
			$tipoCampo = $aCX[4];
			if ($tipoCampo == 'hidden') {
				$sHtml .= "\t\t\t".getHtmlHidden($aCX[0]);
				continue;
			}
			if ($aCX[2]>0) {
		      $sHtml .= "\t\t<td class='lab1' colspan='".$aCX[2]."' valign='top'>".$aCX[1]."</td>\n";
			}
			if ($aCX[3]>0) {
				$sHtml .= "\t\t<td class='txt1' colspan='".$aCX[3]."'>\n";
				if ($sModo == 'vis' or $sModo == 'hid') {
					switch($tipoCampo) {
						case 'textnum' :
							$tipoCampo = 'stringeuro';
							break;
							case 'textarea' :
						case 'selectm' :
							$tipoCampo = 'note';
							break;
						case 'select' :
						case 'radio' :
						case 'radiov' :
						case 'decode' :
							$tipoCampo = 'decode';
							break;
						default : 
							$tipoCampo = 'string';
							break;
					} 
				}
				switch($tipoCampo) {
					case 'radio' :
						$sHtml .= "\t\t\t".getHtmlRadio($aCX[0],$aCX[5]);
						break;
					case 'radiov' :
						$sHtml .= "\t\t\t".getHtmlRadio($aCX[0],$aCX[5],'@REQ',array('aCapo'=>true));
						break;
					case 'select' :
						$sHtml .= "\t\t\t".getHtmlSelect($aCX[0],$aCX[5]);
						break;
					case 'selectm' :
						$sHtml .= "\t\t\t".getHtmlSelectMult($aCX[0],$aCX[5]);
						break;
					case 'checkbox' :
						$sHtml .= "\t\t\t".getHtmlCheckBox($aCX[0],"ok");
						break;
					case 'textcode' :
						$sHtml .= "\t\t\t".getHtmlTextcode($aCX[0],$aCX[5],$aCX[6]);
						break;
					case 'textnum' :
						$sHtml .= "\t\t\t".getHtmlTextNum($aCX[0],$aCX[5],$aCX[6]);
						break;
					case 'date' :
						$sHtml .= "\t\t\t".getHtmlDate($aCX[0])."\n";
						break;
					case 'textarea' :
						$sHtml .= "\t\t\t".getHtmlTextarea($aCX[0],$aCX[5],$aCX[6]);
						break;
					case 'string' :
					case 'stringnum' :
					case 'stringeuro' :
						$sHtml .= "\t\t\t".reqVar($aCX[0])."\n";
						if ($sModo == 'hid') {
							$sHtml .= "\t\t\t".getHtmlHidden($aCX[0]);
						}
						break;
					case 'decode' :
						$sValVis = (isset($aCX[5][reqVar($aCX[0])]))?$aCX[5][reqVar($aCX[0])]:reqVar($aCX[0]);
						$sHtml .= "\t\t\t$sValVis&nbsp;\n";
						if ($sModo == 'hid') {
							$sHtml .= "\t\t\t".getHtmlHidden($aCX[0]);
						}
						break;
					case 'note' :
						$sHtml .= "\t\t\t".getHtmlViewNote(reqVar($aCX[0]))."\n";
						if ($sModo == 'hid') {
							$sHtml .= "\t\t\t".getHtmlHidden($aCX[0]);
						}
						break;
					case 'text' :
					default:
						$sHtml .= "\t\t\t".getHtmlText($aCX[0],$aCX[5],$aCX[6])."\n";
						break;
				}
				$sHtml .= "\t\t</td>\n";
			}
		}
	   $sHtml .= "\t</tr>\n";
	}
	return $sHtml;
}

function getHtmlForm($name='', $action='', $aOpt=array()) {
	$sEncType = ' enctype="multipart/form-data"';//(isset($aOpt['enctype']))?' enctype="multipart/form-data"':'';
	if ($action == '') {
		$action = $_SERVER['PHP_SELF'];
	}
	if ($name == '') {
		$name = 'frm1';
	}
	$sHtml = '<form name="'.$name.'" action="'.$action.'" method="post"'.$sEncType.">\n";
	return $sHtml;
}

function getHtmlTextCode($name, $size, $maxLen=0, $val='@REQ', $aOpt=array()) {
	if (!isset($aOpt['style'])) {
		$aOpt['style'] = "";
	}
	$aOpt['style'] .= 'font-family:lucida console,courier;';
	$sHtml = getHtmlText($name, $size, $maxLen, $val, $aOpt) ;
	return $sHtml;
}

function getHtmlTextNum($name, $size, $maxLen=0, $val='@REQ', $aOpt=array()) {
	if (!isset($aOpt['style'])) {
		$aOpt['style'] = "";
	}
	$aOpt['style'] .= 'text-align:right;';
	return getHtmlText($name, $size, $maxLen, $val, $aOpt);
} 

function getHtmlText($name, $size, $maxLen=0, $val='@REQ', $aOpt=array()) {
	$sOnChange = (isset($aOpt['onChange']))?' onChange="'.$aOpt['onChange'].'" ':'';
	$sReadOnly = (isset($aOpt['readonly']))?' READONLY':'';
	$sMaxLen = ($maxLen==0)?'':' maxlength="'.$maxLen.'"';
	$sStyle = ' style="'.((isset($aOpt['style']))?$aOpt['style']:'').'"';
	$sVal = ($val=='@REQ')? reqVarHE($name):htmlentities($val);
	$sHtml = '<input type="text" class="inp1" name="'.$name.'" id="'.$name.'" value="'.$sVal.'" size="'.$size.'"'.
		$sMaxLen.$sReadOnly.$sStyle.$sOnChange.">\n";
	return $sHtml;
}

function getHtmlDate($name, $val='@REQ', $nomeForm='frm1', $aOpt=array()) {
	$sReadOnly = (isset($aOpt['readonly']))?' READONLY':'';
	$sOnChange = (isset($aOpt['onChange']))?' onChange="'.$aOpt['onChange'].'" ':'';
	$sStyle = ' style="'.((isset($aOpt['style']))?$aOpt['style']:'').'"';
	$sOpzSubm = ($sOnChange=='')?"":"submOrig=S&";
	$sMaxLen = '10';
	$sVal = ($val=='@REQ')? reqVarHE($name):htmlentities($val);
	$sHtml = "<input type=\"text\" class=\"inp1\" name=\"$name\" id=\"$name\" value=\"$sVal\"".
		" size=\"8\" maxlength=\"10\" $sReadOnly $sOnChange>".
		"&nbsp;<img src='".R2_URL_ROOT."/app/_img/calendario.png' " .
		"style='text-decoration:underline;cursor:pointer;'  onClick=\"apriCalend_$nomeForm$name();\" />\n";
	$sHtml .= "<script>";
	$sHtml .= "function apriCalend_$nomeForm$name() {\n" .
		"dtVal = encodeURIComponent(document.$nomeForm.$name.value);\n".
		"window.open('".R2_URL_ROOT."/app/_com/calendario.php?$sOpzSubm" .
		"nome_form=$nomeForm&nome_campo=$name&dt='+dtVal" .
		",'Calendario','top=200,left=100,width=290,height=240," .
		"resizable=yes,titlebar=no,location=no,location=no,menubar=no,toolbar=no');\n".		
			"}\n";
	$sHtml .= "</script>\n";
	
	return $sHtml;
}

function getHtmlPassword($name, $size, $maxLen=0, $val='@REQ', $aOpt=array()) {
	$sReadOnly = (isset($aOpt['readonly']))?' READONLY':'';
	$sMaxLen = ($maxLen==0)?'':' maxlength="'.$maxLen.'"';
	$sStyle = ' style="'.((isset($aOpt['style']))?$aOpt['style']:'').'"';
	$sVal = ($val=='@REQ')? reqVarHE($name):htmlentities($val);
	$sHtml = '<input type="password" class="inp1" name="'.$name.'" id="'.$name.'" ' .
			' value="'.$sVal.'" size="'.$size.'"'.
		$sMaxLen.$sReadOnly.$sStyle.' style="font-family:lucida console,courier">'."\n";
	return $sHtml;
}

function getHtmlFile($name, $size, $aOpt=array()) {
	$sStyle = ' style="'.((isset($aOpt['style']))?$aOpt['style']:'').'"';
	$sHtml = '<input type="file" class="inp1" name="'.$name.'" id="'.$name.'" size="'.$size."\"$sStyle>\n";
	return $sHtml;
}

function getHtmlTextArea($name, $cols=80, $rows=5, $val='@REQ', $aOpt=array()) {
	$sReadOnly = (isset($aOpt['readonly']))?' READONLY':'';
	$sClass = (isset($aOpt['class']))?$aOpt['class']:'inp1';
	$sStyle = ' style="'.((isset($aOpt['style']))?$aOpt['style']:'').'"';
	$sVal = ($val=='@REQ')? reqVarHE($name):htmlentities($val);
	$sHtml = '<textarea class="'.$sClass.'" name="'.$name.'" id="'.$name.'" ' .
			'cols="'.$cols.'" rows="'.$rows.'"'.$sReadOnly.$sStyle.'>'.$sVal.'</textarea>'."\n";
	return $sHtml;
}

function getHtmlViewText($txt) {
	return htmlentities($txt);
}

function getHtmlViewCode($txt) {
	$sHtml = "<span style=\"font-family:lucida console,courier\">".
	getHtmlViewText($txt)."</span>";
	return $sHtml;
}

function getHtmlViewNote($txt) {
	return nl2br(str_replace('  ',' &nbsp;',htmlentities($txt)));
}

function getHtmlViewDate($sAMG,$formatoOut='d/m/Y') {
	return date($formatoOut,strtotime($sAMG));
}

function getHtmlViewNum($num,$nDec,$leftZero=0) {
	return num2str($num,$nDec,$leftZero);
}

function getHtmlRadio($name, $aVal, $val='@REQ', $aOpt=array()) {
	$sVal = ($val==='@REQ')? reqVar($name):$val;
	$sHtml='';
	$sSep = (isset($aOpt['aCapo']) and $aOpt['aCapo'])?"<br>\n":"&nbsp;&nbsp;&nbsp;\n";
	$sOnClick = (isset($aOpt['onClick']) and $aOpt['onClick'])?" onClick=\"".$aOpt['onClick']."\"":"";
	$i = 1;
	foreach($aVal as $vX => $dX) {
		$chk = ($sVal == $vX)?' checked':'';
		$sHtml .= '<input type="radio" id="'.$name."_".$i.'" name="'.$name.'" value="'.htmlentities($vX).'"'.$chk.$sOnClick.
			' class="inp1cb">&nbsp;'.htmlentities($dX).$sSep;
		$i ++;
	}
	return $sHtml;
}

function getHtmlCheckBox($name, $valChecked, $val='@REQ', $aOpt=array()) {
	$sVal = ($val==='@REQ')? reqVar($name):$val;
	$sOnClick = (isset($aOpt['onClick']) and $aOpt['onClick'])?" onClick=\"".$aOpt['onClick']."\"":"";
	$sHtml='';
	$chk = ($sVal == $valChecked)?' checked':'';
	$sHtml .= '<input type="checkBox" id="'.$name.'" name="'.$name.'" ' .
			'value="'.htmlentities($valChecked).'" '.$chk.' class="inp1cb" '.$sOnClick.">\n";
	return $sHtml;
}

function getHtmlSelect($name, $aVal, $val='@REQ', $aOpt=array()) {
	$sVal = ($val==='@REQ')? reqVar($name):$val;
	$sOnChange = (isset($aOpt['onChange']))?' onChange="'.$aOpt['onChange'].'"':'';
	$sStyle = ' style="'.((isset($aOpt['style']))?$aOpt['style']:'').'"';
	$sHtml='<select class="inp1" id="'.$name.'" name="'.$name.'"'.$sOnChange.$sStyle.'>';
	foreach($aVal as $vX => $dX) {
		$sel = ($sVal == $vX)?' selected':'';
		$sHtml .= '<option value="'.htmlentities($vX).'"'.$sel.'>'.htmlentities($dX).'</option>';
	}
	$sHtml.='</select>'."\n";
	return $sHtml;
}

function getHtmlSelectMult($name, $aVal, $val='@REQ', $aOpt=array()) {
	$sVal = ($val==='@REQ')? reqVar($name):$val;
	$sOnChange = (isset($aOpt['onChange']))?' onChange="'.$aOpt['onChange'].'"':'';
	$sStyle = ' style="'.((isset($aOpt['style']))?$aOpt['style']:'').'"';
	$sHtml='<select class="inp1" id="'.$name.'_cmbx" name="'.$name.'_cmbx"'.' size="5"' .
			' onClick="setSelMult_'.$name.'();">';
	foreach($aVal as $vX => $dX) {
		$sHtml .= '<option value="'.htmlentities($vX).'">'.htmlentities($dX).'</option>';
	}
	$sHtml.='</select'.$sStyle.'>'."\n";
	$sHtml .= '<textarea class="inp1" name="'.$name.'" id="'.$name.'" cols="40" rows="5" readonly>'.
		$sVal.'</textarea>'."\n";
	$sHtml .= "<script>\n" .
			"function setSelMult_$name() {\n" .
			"   oTesto = document.getElementById('$name');\n" .
			"   oCombo = document.getElementById('".$name."_cmbx');\n" .
			"   selk = oCombo.selectedIndex;\n".
			"   tnew = oCombo.options[selk].value;\n".
			"   //alert(oTesto.value+tnew);\n".
			"   if (oTesto.value.search(tnew+';') < 0) {\n".
			"	   oTesto.value = oTesto.value+oCombo.options[selk].value+'; ';\n".
			"   } else {\n".
			"     oTesto.value = oTesto.value.replace(tnew+';','');\n".			
			"   }\n".
			"   oTesto.value = oTesto.value.replace('  ',' ');\n".			
			"}\n" .
			"</script>\n";
	return $sHtml;
}

function getHtmlHidden($name, $val='@REQ', $aOpt=array()) {
	$sVal = ($val==='@REQ')? reqVarHE($name):htmlentities($val);
	$sHtml = '<input type="hidden" name="'.$name.'" value="'.$sVal.'">'."\n";
	return $sHtml;
}

function testaHtml($tit2, $msgPag, $prmHelp='', $aMenu=null,$aOpz=array()) {
	global $objSess, $objDB, $isReloadable, $isPopUp, $sXSNQS ;
	$coloreSfondo = R2_GR2_COLORE_SFONDO;
	$coloreTesto = R2_GR2_COLORE_TESTO;
	if (!isset($aOpz['no-cache']) or $aOpz['no-cache']) {
		header("Cache-Control: no-cache, must-revalidate");
		header("Pragma: no-cache");
		header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
	}
	$nX = hexdec(substr($coloreTesto,0,2))+hexdec(substr($coloreTesto,2,2))+hexdec(substr($coloreTesto,4,2));
	$fontFamily=($nX<512)?"arial":"arialbd";
	$tit1 = R2_TITOLO1;
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta Name="expires" CONTENT= "Mon, 1 Jan 2000 00:00:00 GMT">
<meta HTTP-EQUIV="Pragma" CONTENT="no-cache">
<meta HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
<meta HTTP-EQUIV="content-language" content="it">
<meta HTTP-EQUIV="Content-Type" content="text/html; charset=iso-8859-1">
<!-- link rel="shortcut icon" href='../_img/rint2.ico"?>' type="image/x-icon" -->
<link rel="stylesheet" href="<?=R2_URL_ROOT?>/app/_com/css.stilePar.php?cs=<?=$coloreSfondo?>&ct=<?=$coloreTesto?>" type="text/css">
<title><?=$tit1?></title>
</head>
<body style='font-family:arial;margin:5;'>
<table width='990px' align='center' cellpadding='0' cellspacing='0'>
	<tr>
		<td>
			<table border="0" width="100%" cellpadding="0" cellspacing="0" 
				style='background-color:#<?=$coloreSfondo?>;color:#FFFFFF;'>
			   <tr>
			      <td width='1%' style='padding:2px;'><img src='<?= R2_URL_ROOT?>/app/_img/logoRint2.jpg'/></td>
			      <td style='font-size:14pt;' align='left'>
			         &nbsp;<b><?=$tit1?></b><br/>
			         &nbsp;<?=$tit2?>
			      </td>
			   </tr>
			</table>
			<table width='100%' align='center' cellpadding='0' cellspacing='0'>
			   <tr>
			      <td>
						<table border="0" width="100%" cellpadding="3" cellspacing="0" align='center'>
						   <tr>
						      <td class='cmd1'>
<?	
	if (is_object($objSess)) {
		echo $objSess->nome;
	} else {
		echo "-- fuori sessione --";
	}
?>
						      </td>
						      <td class='cmd1' style='text-align:right;'>
<?
	if (isset($isReloadable) and $isReloadable and (!isset($isPopUp) or !$isPopUp)) {
?>
			         &nbsp;&nbsp;&nbsp;
			         <a style='font-size:8pt;' href="<?=R2_URL_ROOT."/app/_com/cambiaPwd.php?xsn=".XSN?>" 
			         	 class='cmd1'>cambia password</a>
			         &nbsp;&nbsp;&nbsp;
			         <a style='font-size:8pt;' href="<?=R2_URL_ROOT."/app/_com/cloneSession.php?xsn=".XSN?>" 
			         	target="_blank" class='cmd1'>nuova finestra</a>
			         &nbsp;&nbsp;&nbsp;
			         <a style='font-size:8pt;' href="<?=R2_URL_ROOT."/app/_com/logOut.php?xsn=".XSN?>" 
			         	class='cmd1'>log-out</a>
<? 
	} 
?>
									&nbsp;&nbsp;&nbsp;
			         			<a style='font-size:8pt;' href="<?=R2_URL_ROOT."/app/_com/RINT2_istruzioni_richiedente.pdf"?> "
			         				target="_blank" class='cmd1'>istruzioni</a>
						      </td>
							</tr>
						</table>
						<hr style='margin:0;border:solid 1px #808080;'/>
						<table border="0" width="100%" cellpadding="3" cellspacing="0" align='center'>
						   <tr>
						      <td class='cmd1'>
<?
	if (is_array($aMenu)) {
		foreach($aMenu as $testo => $aMenX) {
			if (!isset($aMenX['url'])) continue;
			if (isset($aMenX['aTU'])) {
				if (!in_array($objSess->tipo_utente,$aMenX['aTU'])) continue;
			}
			$sTip = (isset($aMenX['tip'])) ? " title=\"".$aMenX['tip']."\" " : "";
			$url = $aMenX['url'];
			$target = (isset($aMenX['target']))?" target='".$aMenX['target']."'":"";
			$sStyle = (isset($aMenX['color']))?"color:".$aMenX['color'].";":"";
?>
			         <a href="<?=$url?>" class='cmd1' style='font-size:8pt;<?=$sStyle?>'<?=$target.$sTip?>>
			         	<?=$testo?></a>&nbsp;&nbsp;&nbsp;
<?
		}
	}
?>&nbsp;
					      </td>
					      <td class='cmd1' align='right' valign='top'>
<?php 
echo "data cons.: ".YMD2DMY(R2_DATA_CONSOLID)." - ";
echo str_replace(" ","&nbsp;",file_get_contents(R2_FS_WEB_ROOT."/app/_inc/versione.txt"));?>
					      </td>
						</tr>
					</table>
				</td>
		   </tr>
		</table>
		<table width='100%' align='center' cellpadding='0' cellspacing='0' border='0'>
		   <tr>
		      <td class='err1' align='center'>
		      	<?echo (trim($msgPag)=='')?'&nbsp;':$msgPag?>
		      </td>
		   </tr>
		</table>
<?
	// la chiusura della td/tr/table e' nella function codaHtml
	if (false) { // mai ! - serve per eliminare la segnalazione di tag non chiuso
?>
	</td></tr></table>
<?php 
	}
}
function codaHtml() {
	global $objSess, $objDB, $aDebugParam;
	$sHTML = "</td></tr></table>\n";
	$flagDisplay='N';
	if (is_array($aDebugParam)) {
		if (isset($aDebugParam['users']['*']))
			$flagDisplay = $aDebugParam['users']['*'];
		if (is_object($objSess) and isset($aDebugParam['users'][$objSess->login]))
			$flagDisplay = $aDebugParam['users'][$objSess->login];
		if ($objSess->backDoor) $flagDisplay = 'D';
		if ($flagDisplay=='D') {
			$bDisplayGet=(isset($aDebugParam['get']) and $aDebugParam['get'] === true)?true:false;
			$bDisplayPost=(isset($aDebugParam['post']) and $aDebugParam['post'] === true)?true:false;
			$bDisplayUser=(isset($aDebugParam['user']) and $aDebugParam['user'] === true)?true:false;
			$bDisplaySess=(isset($aDebugParam['session']) and $aDebugParam['session'] === true)?true:false;
		}
	}	
   if ($flagDisplay=='D') {
      $sHTML .=  "<pre width='132'>\r\n-------- ELAB-INFO - id_utente: ".$objSess->idUtente." - '".LOGIN."' - DB: ".R2_DB_NAME."(".R2_DB_HOST.")\r\n";;
      $fileLog = R2_FS_LOG_DIR."/lasterror";
      if (file_exists($fileLog)) {
      	$sHTML .= "*** ERRORI ***\r\n".file_get_contents($fileLog);
         @unlink($fileLog);
      } 
      $fileLog = R2_FS_LOG_DIR."/lastwarning";
      if (file_exists($fileLog)) {
      	$sHTML .= "*** WARNING/NOTICE ***\r\n".file_get_contents($fileLog);
         @unlink($fileLog);
      } 
      $fileLog = R2_FS_LOG_DIR."/lastdebug";
      if (file_exists($fileLog)) {
      	$sHTML .= "*** DEBUG ***\r\n".file_get_contents($fileLog);
         @unlink($fileLog);
      } 
      $sHTML .= "\r\n";
      if ($bDisplayGet) {
         if (empty($_GET)) {
            $sHTML .=  "<b>GET</b> (vuoto)\r\n";
         } else {
            $sHTML .=  "<b>GET</b> ";
            $sHTML .=  print_r($_GET,1);
         }	
      	$sHTML .= "\r\n";
      }
      if ($bDisplayPost) {
         if (empty($_POST)) {
            $sHTML .=  "<b>POST</b> (vuoto)\r\n";
         } else {
            $sHTML .=  "<b>POST</b>\n";
            $sHTML .=  print_r($_POST,1);
         }
      	$sHTML .= "\r\n";
      }
      if ($bDisplayUser and !$bDisplaySess and is_object($objSess)) {
         $sHTML .=  "<b>OGGETTO UTENTE</b>\n";
         $sHTML .=  print_r($objSess,1);
      	$sHTML .= "\r\n";
      }
      if ($bDisplaySess) {
         $sHTML .=  "<b>SESSIONE</b>\n ";
         $sHTML .=  print_r($_SESSION,1);
      	$sHTML .= "\r\n";
      }
      $sHTML .=  "</pre>\r\n";
   }
	$sHTML .= "</BODY>\n";
	if (isset($objSess->mcrTmStrt)) {
		$sHTML .= "<SCRIPT>\n";
		$scnds = number_format(microtime(true)-$objSess->mcrTmStrt,3,',','.');
		$sHTML .= "   window.status = '$scnds sec.';\n";
		$sHTML .= "</SCRIPT>\n";	
	}
	$sHTML .= "</body>\n</html>\n";
	echo $sHTML;
}

function urlTorna($msgbck="") {
	global $objSess, $isReloadable;
	if ($isReloadable!==false) $isReloadable = true;
	$back = ($isReloadable)?-1:0;
	if ($objSess->hasHistory()) {
		return $objSess->getHistory($back)."&msgbck=".urlencode($msgbck);
	}
	return '';
}         

/*
 * restituisce l'URL della pagina precedente che contiene la stringa sPag
 */
function urlTornaPag($sPag, $msgbck="") {
	global $objSess, $isReloadable;
	if ($isReloadable!==false) $isReloadable = true;
	if ($objSess->hasHistory()) {
		return $objSess->getHistoryPag($sPag)."&msgbck=".urlencode($msgbck);
	}
	return '';
}         

function cmdTorna($testo='',$nSpazi=3) {
	global $objSess, $isReloadable, $isPopUp;
	if ($isPopUp) {
		return $testo;
	}
	$sURL = urlTorna();
	if ($sURL == "") {
		return "";
	}
	if (empty($testo)) $testo = 'torna';
	return str_repeat('&nbsp;',$nSpazi)."<a class='cmd1' href=\"".
			$sURL."\">$testo</a>\n";
}         

function cmdTornaPag($sPag, $testo='',$nSpazi=3) {
	global $objSess, $isReloadable, $isPopUp;
	if ($isPopUp) {
		return $testo;
	}
	$sURL = urlTornaPag($sPag);
	if ($sURL == "") {
		return "";
	}
	if (empty($testo)) $testo = 'torna';
	return str_repeat('&nbsp;',$nSpazi)."<a class='cmd1' href=\"".
			$sURL."\">$testo</a>\n";
}         

function backOnErr($msg) {
	global $objSess, $isReloadable;
	if ($isReloadable!==false) $isReloadable = true;
	$back = ($isReloadable)?-1:0;
	header("location: ".$objSess->getHistory($back)."&msgbck=".urlencode($msg."<br>"));
	exit();	
}

/**
 * Genera html per paginazione
 * $link = url pagina
 * $numRec = numero record totali
 * $startOffset (output)
 * $endOffset (output)
 */
function rint2GetPaginaz($link, $numRec, &$startOffset, &$endOffset){
	global $objSess,$objDB,$sXSNQS;
	$pathPage = $_SERVER['PHP_SELF'];
	$nameScript = substr(basename($_SERVER['PHP_SELF']),0,-4);
	if ($link=='') {
		$link = $pathPage."?".$sXSNQS;
	}
	$startOffset = intval(reqVar('startoffset'));
	$recPerPag = 15;
	// legge da base_parametro il num. rec. per pagina
	if (is_object($objSess)) {
		$sSQL = " SELECT valore FROM parametro " .
				" WHERE id_utente = ".$objSess->idUtente.
				" AND sezione = 'paginaz' AND codice = '".addslashes($nameScript)."'";
		//dbg_($sSQL);
		$objDB->query($sSQL,'recxpag');
		if ($row=$objDB->fetch('recxpag')) {
			//dbg_($row);
			$recPerPag = intval($row['valore']);
		}
		if ($recPerPag < 5) $recPerPag = 5;
	}
	// se passato recperpag modifica il numero rec. per pagina e lo memorizza
	if (reqVar('recperpag')=='piu5' and $recPerPag < 100) {
		$recPerPag += 5;
		if ($recPerPag == 15) {
			$sSQL = " DELETE FROM parametro " .
					" WHERE id_utente = ".$objSess->idUtente.
					" AND sezione = 'paginaz' AND codice = '".addslashes($nameScript)."'";
			$objDB->exec($sSQL);
		} else {
			$sSQL = " REPLACE parametro SET valore = $recPerPag, " .
					" id_utente = ".$objSess->idUtente.
					" , sezione = 'paginaz' , codice = '".addslashes($nameScript)."'";
			$objDB->exec($sSQL);
		}
	}
	if (reqVar('recperpag')=='meno5' and $recPerPag > 5) {
		$recPerPag -= 5;
		if ($recPerPag == 15) {
			$sSQL = " DELETE FROM parametro " .
					" WHERE id_utente = ".$objSess->idUtente.
					" AND sezione = 'paginaz' AND codice = '".addslashes($nameScript)."'";
			$objDB->exec($sSQL);
		} else {
			$sSQL = " REPLACE parametro SET valore = $recPerPag, " .
					" id_utente = ".$objSess->idUtente.
					" , sezione = 'paginaz' , codice = '".addslashes($nameScript)."'";
			$objDB->exec($sSQL);
		}
	}
	
	$endOffset = $startOffset + $recPerPag;
	if ($endOffset > $numRec) $endOffset = $numRec;
	$lastPage = ceil($numRec / $recPerPag);
	$currPage = ceil(($startOffset+1) / $recPerPag);
	if ($currPage < 4) {
		$iStart = 1; 
	} else {
		$iStart = $currPage - 3; 
	}
	$iEnd = $iStart + 6;
	if ($iEnd > $lastPage) $iEnd = $lastPage;
	if  ($iEnd > 6) $iStart = $iEnd - 6;
	//dbg_("startOffset=$startOffset endOffset=$endOffset recPerPag=$recPerPag lastPage=$lastPage" .
	//		" currPage=$currPage iStart=$iStart iEnd=$iEnd");
	$sHTML = "";
	// aggiunge var. GET filtro
	$linkPf = $link."&pagifilt=".urlencode(reqVar('pagifilt'));
	$sHTML .= "$numRec record - pag. $currPage di $lastPage &nbsp;&nbsp;&nbsp;";
	if ($lastPage > 1) {
		if ($currPage > 1) {
			$sHTML .= "<a class='cmd1' href=\"$linkPf&startoffset=0\" title=\"prima pagina\">" .
					  "<img src='".R2_URL_ROOT."/app/_img/freccia_stop_sx.gif' border='0'></a>&nbsp;";
			$offset = ($currPage - 2) * $recPerPag;
			$sHTML .= "<a class='cmd1' href=\"$linkPf&startoffset=$offset\" title=\"pagina precedente\">" .
					  "<img src='".R2_URL_ROOT."/app/_img/freccia_sx.gif' border='0'></a>&nbsp;";
		} else {
			$sHTML .= "<img src='".R2_URL_ROOT."/app/_img/freccia_stop_sx_grigia.gif' border='0'>&nbsp;";
			$sHTML .= "<img src='".R2_URL_ROOT."/app/_img/freccia_sx_grigia.gif' border='0'>&nbsp;";
		} 
		for($i=$iStart;$i<=$iEnd;$i++) {
			if ($i == $currPage) {
				$sHTML .= "<b>$i</b>&nbsp;";
				continue;
			}
			$offset = ($i - 1)* $recPerPag;
			$sHTML .= "<a class='cmd1' href=\"$linkPf&startoffset=$offset\" title=\"pagina $i\">$i</a>&nbsp;";
		}
		if ($currPage < $lastPage) {
			$offset = $currPage * $recPerPag;
			$sHTML .= "<a class='cmd1' href=\"$linkPf&startoffset=$offset\" title=\"pagina successiva\">" .
					  "<img src='".R2_URL_ROOT."/app/_img/freccia_dx.gif' border='0'></a>&nbsp;";
			$offset = ($lastPage - 1) * $recPerPag;
			$sHTML .= "<a class='cmd1' href=\"$linkPf&startoffset=$offset\" title=\"ultima pagina\">" .
					  "<img src='".R2_URL_ROOT."/app/_img/freccia_stop_dx.gif' border='0'></a>";
		} else {
			$sHTML .= "<img src='".R2_URL_ROOT."/app/_img/freccia_dx_grigia.gif' border='0'>&nbsp;";
			$sHTML .= "<img src='".R2_URL_ROOT."/app/_img/freccia_stop_dx_grigia.gif' border='0'>";
		} 
	}
	$sHTML .= "&nbsp;&nbsp;&nbsp;";
	if ($recPerPag > 5 ) {
		$sHTML .= "<a class='cmd1' href=\"$linkPf&startoffset=0&recperpag=meno5\" title=\"diminuisce a ".($recPerPag-5)." il numero righe per pagina\">".
			      "<img src='".R2_URL_ROOT."/app/_img/meno.gif' border='0'></a>&nbsp;";
	}
	if ($recPerPag < 100 ) {
		$sHTML .= "<a class='cmd1' href=\"$linkPf&startoffset=0&recperpag=piu5\" title=\"aumenta a ".($recPerPag+5)." il numero righe per pagina\">" .
			      "<img src='".R2_URL_ROOT."/app/_img/piu.gif' border='0'></a>";
	}
	$sHTML .= "&nbsp;";
	return $sHTML;
}

function rint2GetFiltro($link='') {
	global $objSess,$objDB,$sXSNQS;
	$pathPage = $_SERVER['PHP_SELF'];
	if ($link=='') {
		$link = $pathPage."?".$sXSNQS;
	}
	$sHTML = "";
	//$sHTML .= "<table cellpadding=\"3\" cellspacing=\"3\" class=\"tabl01\" width=\"100%\">\n";
	//$sHTML .= "<tr>\n";
	//$sHTML .= "<td class='txt1' align='left'>";
	$sHTML .= "Filtro:\n";
	$sHTML .= "<input type='text' name='pagifilt' id='pagifilt' value=\"".reqVarHE('pagifilt').
		"\" size='20' class='inp1'>&nbsp;\n";
	$sHTML .= "<a class='cmd1' href='#' onclick=\"javascript:oPgFlt=document.getElementById('pagifilt');" .
		"document.location.href='$link&pagifilt='+encodeURI(oPgFlt.value)+'&startoffset=0';\" " .
		"title=\"imposta filtro\" style='cursor:pointer;text-decoration:underline;'>" .
		"imposta</a>&nbsp;\n";
	$sHTML .= "<a class='cmd1' href='#' onclick=\"document.location.href='$link&pagifilt=&startoffset=0';\" " .
		"title=\"annulla filtro\" style='cursor:pointer;text-decoration:underline;'>" .
		"annulla</a>&nbsp;\n";
	//$sHTML .= "</td>\n";
	//$sHTML .= "</tr>\n";
	//$sHTML .= "</table>\n";
	return $sHTML;
}
	
function getTestaPopUpHtml($tit1,$msgPag="",$aOpt=array()) {
	global $objSess, $objDB, $isReloadable, $isPopUp, $sXSNQS ;
	$coloreSfondo = (is_object($objSess))?$objSess->coloreSfondo:R2_GR2_COLORE_SFONDO;
	$coloreTesto = (is_object($objSess))?$objSess->coloreTesto:R2_GR2_COLORE_TESTO;
	if (defined("R2_FORZA_COLORE_SFONDO")) $coloreSfondo = R2_FORZA_COLORE_SFONDO;
	if (defined("R2_FORZA_COLORE_TESTO")) $coloreTesto = R2_FORZA_COLORE_TESTO;
	$nX = hexdec(substr($coloreTesto,0,2))+hexdec(substr($coloreTesto,2,2))+hexdec(substr($coloreTesto,4,2));
	$fontFamily=($nX<512)?"arial":"arialbd";
	$width = (isset($aOpt['width'])) ? $aOpt['width'] : "";
	$sHtml = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n";
	$sHtml .= "<html>\n";
	$sHtml .= "<head>\n";
	$sHtml .= "<meta Name='expires' CONTENT='Mon, 1 Jan 2000 00:00:00 GMT'>\n";
	$sHtml .= "<meta HTTP-EQUIV='Pragma' CONTENT='no-cache'>\n";
	$sHtml .= "<meta HTTP-EQUIV='Cache-Control' CONTENT='no-cache'>\n";
	$sHtml .= "<meta HTTP-EQUIV='content-language' content='it'>\n";
	$sHtml .= "<meta HTTP-EQUIV='Content-Type' content='text/html; charset=iso-8859-1'>\n";
	$sHtml .= "<!-- link rel='shortcut icon' href='".R2_URL_ROOT
		."/app/_img/rint2.ico' type='image/x-icon' -->\n";
	$sHtml .= "<link rel='stylesheet' href='".R2_URL_ROOT
		."/app/_com/css.stilePar.php?cs=$coloreSfondo&ct=$coloreTesto' type='text/css'>\n";
	$sHtml .= "<title>$tit1</title>\n";
	$sHtml .= "</head>\n";
	$sHtml .= "<body onload='window.focus();'>\n";
	$sHtml .= "<table border='0' width='$width' cellpadding='0' cellspacing='0' "
		."style='background-color:#$coloreSfondo;' align='center'>\n";
	$sHtml .= "\t<tr>\n";
	$sHtml .= "\t\t<td width='1%' style='padding:2px;'>".getHtmlImgLogo($idIstanza,1,"height='30'")."</td>\n";
	$sHtml .= "\t\t<td>\n";
	$sHtml .= "\t\t\t".getHtmlImgGen($tit1, 300, 22, $coloreSfondo, $coloreTesto, 16, $fontFamily)."\n";
	$sHtml .= "\t\t</td>\n";
	if ($objSess->numLogo == '2') {
		$sHtml .= "\t\t<td width='1%' style='padding:8px;'>\n"
			.getHtmlImgLogo($idIstanza,2,"height='30'")."</td>\n";
	}
	$sHtml .= "\t</tr>\n";
	$sHtml .= "</table>\n";
	$sHtml .= "<div class='err1' align='center'>$msgPag</div>\n";
	return $sHtml;
}

function getStylePriorita($priorita) {
	switch($priorita) {
		case 1:
			return "color:grey;font-weight:bold;";
		case 2:
			return "color:grey;";
		case 4:
			return "color:red;";
		case 5:
			return "color:red;font-weight:bold;";
		case 3:
		default:
			return "";
	}
}

